package com.ls.rabbitmqService.topic;

import com.ls.rabbitmqService.utils.ConnectionFactoryUtil;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.TimeoutException;

public class TopicSend {

    private static final Logger logger = LoggerFactory.getLogger(TopicSend.class);


    public static void send(String exchanges_name,String routingKey,String msg){
        Connection connection =null;
        Channel channel=null;
        try {
            connection=ConnectionFactoryUtil.getRabbitConnection();
            channel = connection.createChannel();
            /**
             * 采用主题和分发模式  数据是没有历史数据的
             *
             * 其实在上面2模式下交换器采用持久化好像没啥意义因为 消息没有历史数据，当消费者不在线时
             */
            channel.exchangeDeclare(exchanges_name, BuiltinExchangeType.TOPIC,true);


            channel.queueDeclare("queue.topic.test",true,false,false,null);

            channel.queueBind("queue.topic.test",exchanges_name,routingKey);

            //发送消息时设置消息的持久化
            AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
            //这里的deliveryMode=1代表不持久化，deliveryMode=2代表持久化。
            builder.deliveryMode(2);
            AMQP.BasicProperties properties = builder.build();

            channel.basicPublish(exchanges_name,routingKey,properties,msg.getBytes());
            logger.info(String.format("send routingKey:%s,message:%s",routingKey,msg));
        } catch (IOException e) {
            logger.error(e.getMessage(),e);
        } finally{
            if(channel!=null){
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public static void batchSend(String exchanges_name,String routingKey,String... msgs){
        Connection connection =null;
        Channel channel=null;
        try {
            connection=ConnectionFactoryUtil.getRabbitConnection();
            channel = connection.createChannel();
            /**
             * 采用主题和分发模式  数据是没有历史数据的
             */
            channel.exchangeDeclare(exchanges_name, BuiltinExchangeType.TOPIC);
            for(String msg:msgs)
                channel.basicPublish(exchanges_name,routingKey,null,msg.getBytes());
            logger.info(String.format("batchsend routingKey:%s,messages:%s",routingKey,Arrays.asList(msgs)));
        } catch (IOException e) {
            logger.error(e.getMessage(),e);
        } finally{
            if(channel!=null){
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    private static final String EXCHANGES_NAME="topic_logs";

    public static void main(String[] args){

        Connection connection =null;
        Channel channel=null;
        try {
            connection=ConnectionFactoryUtil.getRabbitConnection();
            channel = connection.createChannel();

            /**
             * 采用主题和分发模式  数据是没有历史数据的
             */
            channel.exchangeDeclare(EXCHANGES_NAME, BuiltinExchangeType.TOPIC);

            String[] routingKeys=new String[]{"word"};
            for(String routingKey:routingKeys){
                String message=String.format("From routingKey:%s message!",routingKey);
                channel.basicPublish(EXCHANGES_NAME,routingKey,null,message.getBytes());
                System.out.println(String.format("TopicSend [x] send routingKey:%s,message:%s",routingKey,message));
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if(channel!=null){
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}
